package com.itcast.cn.response;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;

/**
 * 解决字节流
 */
@WebServlet("/responseDemo5")
public class ResponseDemo5 extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //设置编码格式
        response.setContentType("text/html;charset=utf-8");

        //2，获取输出流
        ServletOutputStream outputStream = response.getOutputStream();

        outputStream.write("你好吗？".getBytes("utf-8"));

//        //1,读取文件
//        try {
//            InputStream stream = ResponseDemo5.class.getClassLoader().getResourceAsStream("18.jpg");
//            //2，获取输出流
//            ServletOutputStream outputStream = response.getOutputStream();
//
//
//            //3.完成流的拷贝
//            byte[] buff = new byte[1024];
//            int len = 0;
//            while ((len=stream.read(buff))!=-1) {
//                outputStream.write(buff,0,len);
//            }
//            stream.close();
//
//        } catch (Exception e) {
//            e.printStackTrace();
//        }


    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}
